Today we mainly studied the basic components of java programing, including datatypes, syntax features, and memory allocation.
First, let's recap what we learned:do you still remember what the following piece of code means?
publicstaticvoid main(){}
"Public" makes the functions accessible by all the users. There are two other similar key words: One is "protected", which restricts the accessibility only to the "family" of the class; the other is "private", which means the function can only be called by the "direct relatives" of the class.
"Static" makes the function belongs to the class, but not any instances of it. Therefore we can call the function without instantiate the class.
"Void" tells us that the function only returns "void". "void" is the java equivalent of python's "None".
This line is a required component of every java code, so it's good to remember them.
Now let's look at how java programmer name their variables:
The naming convention is quite similar to python --- only numbers, _, and characters are allowed.
However, the brackets may cause some confusion:
[ ] is NOT a list that can hold any object anymore. It IS an array. In variable declaration, we should write it as, for example, "int[] count = {0,0,0,0,0,0,0,0,0,0}; ".The "int" here shows that the array is composed of integers. That place may also be float, bool, etc. Note that in the array, there could be only ONE TYPE of variables.
{ } brackets the main codes under the function. It's recommended to align the two brackets but not required.
Keep in mind that every sentence should end with a ";", as indentation doesn't mean anything in java!
There are many useful methods in java.utl. One example is java.utl.Scanner, which can read both inputs and files. Scanner is actually a class, which means it needs to be instantiated to use.
There's also a package called system. One of it's various methods is System.out.println(). It can print the string in he bracket and start a new line. "println" is short for "printline". Here, "out" is an object in "System"; their relationship is like "Point" in a "Rectangle".
These are all feature of a OOP programming language. Everything are class or instances or methods.
Since Java is a static language, we must declare them before using. The reason behind this is that doing so can improve efficiency. This is how it works:
Different datatypes are given different memory spaces. In other words, we are allocating the least possible memory space to the variables.
For instance, the byte type data is given eight bits of memory. Therefore if can store 2^7(one space for the sign) different integers. There are two ways to represent the data:
"Sign and Magnitude": one place for sign, the others are binary representation of the number. The range is hence -127~+127. (0 is repeated twice)
"two's complement": the first bit represents -128, and the others are the binary representation of the difference of the integer and -128. The range is hence -128~127.
(Today's computer normally use the second method, but for convenience, we will study the first one only.)
For more information, please refer to the book Java4python chapter one and two.
First, let's recap what we learned:do you still remember what the following piece of code means?
"Public" makes the functions accessible by all the users. There are two other similar key words: One is "protected", which restricts the accessibility only to the "family" of the class; the other is "private", which means the function can only be called by the "direct relatives" of the class.
"Static" makes the function belongs to the class, but not any instances of it. Therefore we can call the function without instantiate the class.
"Void" tells us that the function only returns "void". "void" is the java equivalent of python's "None".
This line is a required component of every java code, so it's good to remember them.
Now let's look at how java programmer name their variables:
The naming convention is quite similar to python --- only numbers, _, and characters are allowed.
However, the brackets may cause some confusion:
- [ ] is NOT a list that can hold any object anymore. It IS an array. In variable declaration, we should write it as, for example, "int[] count = {0,0,0,0,0,0,0,0,0,0}; ".The "int" here shows that the array is composed of integers. That place may also be float, bool, etc. Note that in the array, there could be only ONE TYPE of variables.
- { } brackets the main codes under the function. It's recommended to align the two brackets but not required.
Keep in mind that every sentence should end with a ";", as indentation doesn't mean anything in java!There are many useful methods in java.utl. One example is java.utl.Scanner, which can read both inputs and files. Scanner is actually a class, which means it needs to be instantiated to use.
There's also a package called system. One of it's various methods is System.out.println(). It can print the string in he bracket and start a new line. "println" is short for "printline". Here, "out" is an object in "System"; their relationship is like "Point" in a "Rectangle".
These are all feature of a OOP programming language. Everything are class or instances or methods.
Since Java is a static language, we must declare them before using. The reason behind this is that doing so can improve efficiency. This is how it works:
Different datatypes are given different memory spaces. In other words, we are allocating the least possible memory space to the variables.
For instance, the byte type data is given eight bits of memory. Therefore if can store 2^7(one space for the sign) different integers. There are two ways to represent the data:
- "Sign and Magnitude": one place for sign, the others are binary representation of the number. The range is hence -127~+127. (0 is repeated twice)
- "two's complement": the first bit represents -128, and the others are the binary representation of the difference of the integer and -128. The range is hence -128~127.
(Today's computer normally use the second method, but for convenience, we will study the first one only.)For more information, please refer to the book Java4python chapter one and two.